﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class ProcedureTmpltDAO : IProcedureTmpltDAO
    {
        public bool DeleteProcedureTmplt(ProcedureTmplt clsProcedureTmplt)
        {
            return clsProcedureTmplt.Save("D");
        }

        public IList<ProcedureTmplt> GetProcedureTmplt(ProcedureTmplt clsProcedureTmplt)
        {
            DataTable dt = Db.GetDataTable(clsProcedureTmplt.CreateGetCommandParameters());
            clsProcedureTmplt.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("ProcedureTmplt doesn't exist");
            }
            IList<ProcedureTmplt> list = new List<ProcedureTmplt>();
            list.Add(clsProcedureTmplt);
            return list;
        }

        public IList<ProcedureTmplt> GetProcedureTmplt(string procedureTmplt)
        {
            ProcedureTmplt clsProcedureTmplt = new ProcedureTmplt();
            clsProcedureTmplt._HeadingCode = procedureTmplt;
            return this.GetProcedureTmplt(clsProcedureTmplt);
        }

        public IList<ProcedureTmplt> GetsProcedureTmplt()
        {
            ProcedureTmplt clsProcedureTmplt = new ProcedureTmplt();
            IDataReader dtReader = Db.GetDataReader(clsProcedureTmplt.CreateGetsCommandParameters());
            IList<ProcedureTmplt> list = new List<ProcedureTmplt>();
            while (dtReader.Read())
            {
                clsProcedureTmplt = new ProcedureTmplt();
                clsProcedureTmplt.LoadDataToCollection(dtReader);
                list.Add(clsProcedureTmplt);
            }
            return list;
        }

        public DataTable GetsProcedureTmplt(string SPName, DbParameter[] param)
        {
            ProcedureTmplt clsProcedureTmplt = new ProcedureTmplt();
            return Db.GetDataTable(clsProcedureTmplt.GetsDataReader(SPName, param));
        }

        public bool InsertProcedureTmplt(ProcedureTmplt clsProcedureTmplt)
        {
            return clsProcedureTmplt.Save("I");
        }

        public int InsertProcedureTmpltReturnIdentity(ProcedureTmplt clsProcedureTmplt)
        {
            return clsProcedureTmplt.Save("I", true);
        }

        public string InsertProcedureTmpltReturnOutput(ProcedureTmplt clsProcedureTmplt)
        {
            return clsProcedureTmplt.Save("I", "@ProcedureTmpltID");
        }

        public IList<ProcedureTmplt> ListProcedureTmplt(ProcedureTmplt clsProcedureTmplt)
        {
            IDataReader dtReader = Db.GetDataReader(clsProcedureTmplt.CreateListCommandParameters());
            IList<ProcedureTmplt> list = new List<ProcedureTmplt>();
            while (dtReader.Read())
            {
                ProcedureTmplt clsProcedureTmplts = new ProcedureTmplt();
                clsProcedureTmplts.LoadDataToCollection(dtReader);
                list.Add(clsProcedureTmplts);
            }
            return list;
        }

        public IList<ProcedureTmplt> ListProcedureTmplt(string procedureTmplt)
        {
            ProcedureTmplt clsProcedureTmplt = new ProcedureTmplt();
            clsProcedureTmplt._HeadingCode = procedureTmplt;
            return this.ListProcedureTmplt(clsProcedureTmplt);
        }

        public bool UpdateProcedureTmplt(ProcedureTmplt clsProcedureTmplt)
        {
            return clsProcedureTmplt.Save("U");
        }

        public int UpdateProcedureTmplt(string SPName, DbParameter[] param)
        {
            ProcedureTmplt clsProcedureTmplt = new ProcedureTmplt();
            return Db.ExecuteNonQuery(clsProcedureTmplt.UpdateQuery(SPName, param));
        }
    }
}

